﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MimeTypes.cs" company="">
//   
// </copyright>
// <summary>
//   The mime types.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Skymate.Core.IO
{
    using System;
    using System.Collections.Concurrent;
    using System.Web;

    using Microsoft.Win32;

    using Skymate.Extensions;

    /// <summary>
    /// The mime types.
    /// </summary>
    public static class MimeTypes
    {
        /// <summary>
        /// The _mime map.
        /// </summary>
        private static readonly ConcurrentDictionary<string, string> _mimeMap =
            new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// The map name to mime type.
        /// </summary>
        /// <param name="fileNameOrExtension">
        /// The file name or extension.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string MapNameToMimeType(string fileNameOrExtension)
        {
            return MimeMapping.GetMimeMapping(fileNameOrExtension);
        }

        /// <summary>
        /// Returns the (dotless) extension for a mime type
        /// </summary>
        /// <param name="mimeType">
        /// The mime type
        /// </param>
        /// <returns>
        /// The corresponding file extension (without dot)
        /// </returns>
        public static string MapMimeTypeToExtension(string mimeType)
        {
            if (mimeType.IsEmpty())
            {
                return null;
            }

            return _mimeMap.GetOrAdd(
                mimeType, 
                k =>
                    {
                        string result = null;

                        try
                        {
                            using (
                                var key = Registry.ClassesRoot.OpenSubKey(
                                    @"MIME\Database\Content Type\" + mimeType, 
                                    false))
                            {
                                var value = key != null ? key.GetValue("Extension", null) : null;
                                result = value != null ? value.ToString().Trim('.') : null;
                            }
                        }
                        catch
                        {
                            var parts = mimeType.Split('/');
                            result = parts[parts.Length - 1];
                            switch (result)
                            {
                                case "pjpeg":
                                    result = "jpg";
                                    break;
                                case "x-png":
                                    result = "png";
                                    break;
                                case "x-icon":
                                    result = "ico";
                                    break;
                            }
                        }

                        return result;
                    });
        }
    }
}